<template>
  <!-- 主体内容 -->
  <el-container class='home_container'>
    <el-header>
      <div>
        <img
          src="../assets/heima.png"
          alt=""
        >
        <span>电商后台管理系统</span>
      </div>
      <el-button
        @click="logout"
        type='info'
      >退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' :'200px'">
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409EEF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition='false'
          router
          :default-active='activeUrl'
        >
          <!-- 侧边栏的收缩和展开 -->
          <div
            class='toggle'
            @click="clickCollapse"
          >|||</div>
          <el-submenu
            :index="menu.id +''"
            v-for='menu in menuList'
            :key="menu.id"
          >
            <template slot="title">
              <i :class="iconsObj[menu.id]"></i>
              <span>{{menu.authName}}</span>
            </template>
            <el-menu-item
              :index="'/'+ submenu.path"
              v-for="submenu in menu.children"
              :key='submenu.id'
              @click="saveNavPath('/'+ submenu.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{submenu.authName}}</span>
              </template>
            </el-menu-item>

          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单列表
      menuList: [],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      //   折叠
      isCollapse: false,
      // 激活的url
      activeUrl: ''
    }
  },
  created() {
    this.getMenuList()
    this.activeUrl = window.sessionStorage.getItem('activeUrl')
  },
  methods: {
    logout() {
      console.log(this)
      window.sessionStorage.clear()
      this.$message.success('成功退出登录')
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: result } = await this.$http.get('menus')
      if (result.meta.status !== 200) return this.$message.error(result.meta.msg)
      this.menuList = result.data
      console.log(result.data)
    },
    clickCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavPath(path) {
      window.sessionStorage.setItem('activeUrl', path)
      this.activeUrl = path
    }
  }
}
</script>
<style lang="less" scoped>
.home_container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display:flex;
  justify-content: space-between;
  padding-left: 0;
  color:#fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }

}
.el-aside {
  background-color: #545c64;
  .el-menu {
    border-right: none
  }

}
.el-main {
  background-color: #eaedf1;

}
.iconfont {
  margin-right: 10px
}
.toggle {
  color:#fff;
  background-color:#4a5064;
  font-size:10px;
  line-height:24px;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
